<?php
include "../config/connection.inc";

function checkAdminAuthenticate($username, $pwd){
    $query1 = "select count(USER_ID) as numb from ADMIN where USER_NAME = '$username' and USER_PWD = '$pwd'";
    $query2 = "select count(USER_ID) as numb from MODERATORS where USER_NAME = '$username' and USER_PWD = '$pwd' and IS_ACTIVE = 1";
    $result1 = mysql_fetch_array(mysql_query($query1));
    $result2 = mysql_fetch_array(mysql_query($query2));
    if($result1["numb"] > 0){
        return 1;
    }else if ($result2["numb"] > 0){
        return 2;
    }else{
        return 0;
    }
}

function checkUserAuthenticate($username, $pwd){
    $query1 = "select count(USER_ID) as numb from NORMALUSERS where USER_NAME = '$username' and USER_PWD = '$pwd' and IS_ACTIVE = 1";
    $query2 = "select count(USER_ID) as numb from COMPANIES where USER_NAME = '$username' and USER_PWD = '$pwd' and IS_ACTIVE = 1";
    $result3 = mysql_fetch_array(mysql_query($query1));
    $result4 = mysql_fetch_array(mysql_query($query2));
    if($result3["numb"] > 0){
        return 3;
    }else if ($result4["numb"] > 0){
        return 4;
    }else{
        return 0;
    }
}

function getAdminInfor($username){
    $query = "select USER_ID, DOB, USER_NAME, USER_SEX, USER_PWD, USER_AVATAR, USER_PHONE, USER_ADD, USER_EMAIL, USER_DESCRIPTION, USER_CREATEDATE, USER_TYPE
    from ADMIN
    where USER_NAME = '$username'";
    return mysql_query($query);
}

function getModInfor($username){
    $query = "select USER_ID, DOB,IS_ACTIVE, USER_NAME, USER_SEX, USER_PWD, USER_AVATAR, USER_PHONE, USER_ADD, USER_EMAIL, USER_DESCRIPTION, USER_CREATEDATE, USER_TYPE
    from MODERATORS
    where USER_NAME = '$username'";
    return mysql_query($query);
}

function getCompanyInfor($username){
    $query = "select USER_ID,IS_ACTIVE, USER_NAME, USER_SEX, USER_PWD, USER_AVATAR, USER_PHONE, USER_ADD, USER_EMAIL, USER_DESCRIPTION, USER_CREATEDATE, USER_TYPE, FOUNDING_DATE
    from COMPANIES
    where USER_NAME = '$username'";
    return mysql_query($query);
}

function getUserInfor($username){
    $query = "select USER_ID, DOB,IS_ACTIVE, USER_NAME, USER_SEX, USER_PWD, USER_AVATAR, USER_PHONE, USER_ADD, USER_EMAIL, USER_DESCRIPTION, USER_CREATEDATE, USER_TYPE
    from NORMALUSERS
    where USER_NAME = '$username'";
    return mysql_query($query);
}

function checkUserAvailable($username){
    $query1 = "select count(USER_ID) as numb from NORMALUSERS where USER_NAME = '$username'";
    $query2 = "select count(USER_ID) as numb from COMPANIES where USER_NAME = '$username'";
    $query3 = "select count(USER_ID) as numb from ADMIN where USER_NAME = '$username'";
    $query4 = "select count(USER_ID) as numb from MODERATORS where USER_NAME = '$username'";
    
    $result1 = mysql_fetch_array(mysql_query($query1));
    $result2 = mysql_fetch_array(mysql_query($query2));
    $result3 = mysql_fetch_array(mysql_query($query3));
    $result4 = mysql_fetch_array(mysql_query($query4));
    
    if($result1["numb"] == 0 && $result2["numb"] == 0 && $result3["numb"] == 0 && $result4["numb"] == 0)
        return false;
    return true;
}

function insertUser($user){
    if ($user['type'] == 3)
        $table = "NORMALUSERS";
    else if ($user['type'] == 4)
        $table = "COMPANIES";
    else
        return;
    $query = "INSERT INTO ". $table .
              " VALUES (NULL, '".$user['dob']."', '1', '".$user['username']."', '".$user['sex']."', 
              '".$user['pwd']."', '".$user['avatar']."', '".$user['phone']."', '".$user['add']."',
              '".$user['email']."', '".$user['descript']."', '".$user['createdate']."', '".$user['type']."')";
    $result = mysql_query($query);
    return $result;
}
function insertNormaluser($user){
    $query = "INSERT INTO NORMALUSERS".
              " VALUES (NULL, '".$user['dob']."', '1', '".$user['username']."', '".$user['sex']."', 
              '".$user['pwd']."', '".$user['avatar']."', '".$user['phone']."', '".$user['add']."',
              '".$user['email']."', '".$user['descript']."', '".$user['createdate']."', '4')";
    $result = mysql_query($query);
    return $result;
    
}

function insertCompany($user){
    $query = "INSERT INTO COMPANIES".
              " VALUES (NULL, '".$user['dob']."', '1', '".$user['username']."', '".$user['sex']."', 
              '".$user['pwd']."', '".$user['avatar']."', '".$user['phone']."', '".$user['add']."',
              '".$user['email']."', '".$user['descript']."', '".$user['createdate']."', '3')";
    $result = mysql_query($query);
    return $result;
}

function insertModerator($user){
    $query = "INSERT INTO MODERATORS".
              " VALUES (NULL, '".$user['dob']."', '1', '".$user['username']."', '".$user['sex']."', 
              '".$user['pwd']."', '".$user['avatar']."', '".$user['phone']."', '".$user['add']."',
              '".$user['email']."', '".$user['descript']."', '".$user['createdate']."', '2')";
    $result = mysql_query($query);
    return $query;
}

function getModStatus($keyword){
    //Get username and id with active status - moderator
    $query = "SELECT USER_ID, USER_NAME, USER_TYPE, IS_ACTIVE AS STATUS FROM MODERATORS WHERE USER_NAME LIKE '%$keyword%' ORDER BY USER_TYPE";
    $result = mysql_query($query);
    return $result;
}

function getNormalUserStatus($keyword){
    //Get username and id with active status - normal user
    $query = "SELECT USER_ID, USER_NAME, USER_TYPE, IS_ACTIVE AS STATUS FROM NORMALUSERS WHERE USER_NAME LIKE '%$keyword%' ORDER BY USER_TYPE";
    $result = mysql_query($query);
    return $result;
}
function getCompanyStatus($keyword){
    //Get username and id with active status - company
    $query = "SELECT USER_ID, USER_NAME, USER_TYPE, IS_ACTIVE AS STATUS FROM COMPANIES WHERE USER_NAME LIKE '%$keyword%' ORDER BY USER_TYPE";
    $result = mysql_query($query);
    return $result;
}

function resetPassword($id, $type){
    if($type == 2){
        $query = "UPDATE MODERATORS SET USER_PWD = '123' WHERE USER_ID = $id";    
    }else if($type == 3){
        $query = "UPDATE COMPANIES SET USER_PWD = '123' WHERE USER_ID = $id";
    }else if($type == 4){
        $query = "UPDATE NORMALUSERS SET USER_PWD = '123' WHERE USER_ID = $id";
    }
    $result = mysql_query($query);
}

function changeStatus($id, $type, $status){
    if($type == 2){
        $query = "UPDATE MODERATORS SET IS_ACTIVE = $status WHERE USER_ID = $id";    
    }else if($type == 3){
        $query = "UPDATE COMPANIES SET IS_ACTIVE = $status WHERE USER_ID = $id";
    }else if($type == 4){
        $query = "UPDATE NORMALUSERS SET IS_ACTIVE = $status WHERE USER_ID = $id";
    }
    $result = mysql_query($query);
}

function updateInformation($id, $type, $add, $tel, $email, $description, $dob){
    if($type == 1){
        $query = "UPDATE ADMIN SET USER_ADD = '$add', USER_PHONE = '$tel', USER_EMAIL = '$tel', USER_DESCRIPTION = '$tel', DOB = '$dob' WHERE USER_ID = $id";    
    }else if ($type == 2){
        $query = "UPDATE ADMIN SET USER_ADD = '$add', USER_PHONE = '$tel', USER_EMAIL = '$tel', USER_DESCRIPTION = '$tel', DOB = '$dob' WHERE USER_ID = $id";  
    }else if ($type == 3){
        $query = "UPDATE ADMIN SET USER_ADD = '$add', USER_PHONE = '$tel', USER_EMAIL = '$tel', USER_DESCRIPTION = '$tel', FOUNDING_DATE = '$dob' WHERE USER_ID = $id";  
    }else if ($type == 4){
        $query = "UPDATE ADMIN SET USER_ADD = '$add', USER_PHONE = '$tel', USER_EMAIL = '$tel', USER_DESCRIPTION = '$tel', DOB = '$dob' WHERE USER_ID = $id";  
    }
    $result = mysql_query($query);
}
?>